package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.domain.Banner;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.query.BannerQuery;
import cn.wolfcode.luowowo.service.IBannerService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("banner")
public class BannerController {

    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BannerQuery qo){

        //前端页面共享个page
        // pageinfo :是mybatis插件,操作的是mysql数据库
        // Page  : 是spring-data 的组件,操作的是mongodb
        Page<Banner> page =  bannerService.query(qo);
        model.addAttribute("page",page);
        return "banner/list";
    }

    // 添加按钮中保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner) {
        // 保存和编辑
        bannerService.saveOrUpdate(banner);
        return JsonResult.success();
    }

    // banner模块:添加按钮中:游记和攻略(联动)
    @RequestMapping("/getAllType")
    @ResponseBody
    public Object getAllType() {

        Map<String,Object> map = new HashMap<>();
        List<Travel> travels = travelService.listAll();
        List<Strategy> strategies = strategyService.listAll();
        map.put("ts",travels);
        map.put("sts",strategies);

        return JsonResult.success(map);
    }
}